//  "Show Image"
//
//  Earl F. Glynn, April 1998.  Updated September 1998.

unit ShowImageForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls,
  ExtDlgs,
  ColorLibrary;              // TColorPlane


type

  TRGB32 = packed record
    B, G, R, A: Byte;
  end;
  TRGB32Array = packed array[0..MaxInt div SizeOf(TRGB32)-1] of TRGB32;
  PRGB32Array = ^TRGB32Array;

  
  TFormShow = class(TForm)
    Button1: TButton;
    ImageBig: TImage;
    LabelLocation: TLabel;
    LabelIntensity: TLabel;
    LabelRGB: TLabel;
    LabelHSV: TLabel;
    LabelFilename: TLabel;
    ImageHistogram: TImage;
    RadioGroupColorPlane: TRadioGroup;
    LabelLightness: TLabel;
    RadioGroupColorSpace: TRadioGroup;
    ButtonPrint: TButton;
    LabelAttributes: TLabel;
    LabelColorPlane: TLabel;
    SavePictureDialog: TSavePictureDialog;
    ButtonSave: TButton;
    LabelCMYK: TLabel;
    CheckBoxStretch: TCheckBox;
    LabelHLS: TLabel;
    LabelYIQ: TLabel;
    CheckBoxInvert: TCheckBox;
    CheckBoxMonochrome: TCheckBox;
    LabelUniqueColors: TLabel;
    OpenPictureDialog: TOpenPictureDialog;
    Image1: TImage;
    Button2: TButton;
    Button3: TButton;
    ScrollBar1: TScrollBar;
    Button6: TButton;
    Image3: TImage;
    Button4: TButton;
    Image2: TImage;
    Image4: TImage;
    Button5: TButton;
    Button7: TButton;
    Memo1: TMemo;
    Button8: TButton;

    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RadioGroupColorPlaneClick(Sender: TObject);
    procedure RadioGroupColorSpaceClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ButtonPrintClick(Sender: TObject);
    procedure ButtonSaveClick(Sender: TObject);
    procedure ImageBigMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure CheckBoxStretchClick(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure CheckBoxInvertClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button3Click(Sender: TObject);
   // procedure Button4Click(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
  
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);

   
  private
    ColorPlane:  TColorPlane;
    PROCEDURE UpdateDisplay (CONST ColorPlane:  TColorPlane);
  public
    BitmapPrint:  TBitmap;
    BitmapBig  :  TBitMap;
    Bitmap1Big :  TBitMap;
    BitmapClear:  TBitmap;
  end;

var
  FormShow: TFormShow;

implementation
{$R *.DFM}

uses
{$IFDEF GIF}
  GIFImage,                   // TGIFImage
{$ENDIF}
  RealColorLibrary,           // RGBToHSV
  GraphicsConversionsLibrary, // LoadGraphicFile
  HistogramLibrary,           // THistogram
  ImageProcessingPrimitives,  // GetBitmapDimensionsString
  IniFiles,                   // TIniFile
  ScreenPrint;

  CONST
    KeywordSetup     = 'Setup';
    KeywordDirectory = 'Directory';

procedure TFormShow.Button1Click(Sender: TObject);
  VAR
    Filename:  STRING;
    IniFile :  TIniFile;
    NewPath :  STRING;
begin
  IF   OpenPictureDialog.Execute
  THEN BEGIN
    BitmapBig.Free;
    BitmapBig := TBitmap.Create;
    BitmapBig := LoadGraphicsFile(OpenPictureDialog.Filename);


  // for the computation of Image1
    Bitmap1Big.Free;
    Bitmap1Big := TBitmap.Create;
    Bitmap1Big := LoadGraphicsFile(OpenPictureDialog.Filename);

    Image1.Picture.LoadFromFile(OpenPictureDialog.Filename);


    // Update INI file for next time
    Filename := ChangeFileExt(ParamStr(0), '.INI');
    NewPath := ExtractFilePath(OpenPictureDialog.Filename);
    OpenPictureDialog.InitialDir := NewPath;
    IniFile := TIniFile.Create(Filename);
    TRY
      Inifile.WriteString(KeywordSetup, KeywordDirectory, NewPath)
    FINALLY
      IniFile.Free
    END;

    // Flush INI cache
    WritePrivateProfileString(NIL, NIL, NIL, pChar(Filename));

    LabelFilename.Caption := OpenPictureDialog.Filename;

    // Update this label before forcing PixelFormat
    LabelAttributes.Caption := GetBitmapDimensionsString(BitmapBig);

    // Force to pf24bit if necessary.  Make other logic much more simple by
    // not having to worry aboug various PixelFormats.
    IF   BitmapBig.PixelFormat <> pf24bit
    THEN BitmapBig.PixelFormat := pf24bit;

    // Clear old histogram (some of the following may be unnecessary now that
    // pf24bit is being forced and it's no longer necessary to hide certain
    // features.
    ImageHistogram.Visible := TRUE;
    ImageHistogram.Picture := NIL;
    RadioGroupColorSpace.ItemIndex := 0;  {Select RGB color space}
    RadioGroupColorSpace.Enabled := TRUE;
    RadioGroupColorPlane.Enabled := TRUE;
    ButtonPrint.Enabled := TRUE;
    FormPrint.ButtonHistograms.Enabled := TRUE;
    FormPrint.ButtonColorSpaceArray.Enabled := TRUE;
    RadioGroupColorSpace.Visible := TRUE;
    RadioGroupColorPlane.Visible := TRUE;
    UpdateDisplay(cpRGB);

    LabelUniqueColors.Caption := 'Unique Colors = ' +
                                 IntToStr( CountColors(BitmapBig) );
  END
end;



procedure TFormShow.ImageBigMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
  VAR
    CC,MM,YY,KK:  INTEGER;      // CMYK variables
    Intensity  :  INTEGER;
    Lightness  :  INTEGER;
    RGB        :  TColor;
    RGBTriple  :  TRGBTriple;
    R,G,B      :  BYTE;
    H,S,V, L   :  TReal;
    xActual    :  INTEGER;
    yActual    :  INTEGER;
begin
  IF   CheckBoxStretch.Checked
  THEN BEGIN
    xActual := MulDiv(X, BitmapBig.Width,  ImageBig.Width);
    yActual := MulDiv(Y, BitmapBig.Height, ImageBig.Height)
  END
  ELSE BEGIN
    xActual := X;
    yActual := Y
  END;

  // If bitmap is smaller than image area, only show values if inside bitmap.
  IF   (xActual < ImageBig.Picture.Bitmap.Width) AND
       (yActual < ImageBig.Picture.Bitmap.Height)
  THEN BEGIN

    LabelLocation.Caption :=  '[I, J] = [' + IntToStr(xActual) + ', ' +
                                             IntToStr(yActual) + ']';

    // Get values from original bitmap, which is not necessarily being displayed
    // in RGB format.  Use Pixels so this will work with pf8bit or pf24bit BMPs.
    RGB :=  BitmapBig.Canvas.Pixels[xActual, yActual];
    R := GetRValue(RGB);
    G := GetGValue(RGB);
    B := GetBValue(RGB);
    RGBTriple := ColorToRGBTriple(RGB);

    LabelRGB.Caption := Format('[R, G, B] = [%d, %d, %d] = [%.3f,  %.3f,  %.3f]',
                        [ R, G, B, R/255, G/255, B/255]);

    RGBToHSV(GetRValue(RGB) / 255,
             GetGValue(RGB) / 255,
             GetBValue(RGB) / 255, H, S, V);
    LabelHSV.Caption := Format('[H, S, V] = [%.0f, %.0f, %.0f] = [ %.1f,  %.3f,  %.3f]',
                              [255*H/360, 255*S, 255*V, H, S, V]);

    RGBToHLS(GetRValue(RGB) / 255,
             GetGValue(RGB) / 255,
             GetBValue(RGB) / 255, H, L, S);
    LabelHLS.Caption := Format('[H, L, S] = [%.0f, %.0f, %.0f] = [ %.1f,  %.3f,  %.3f]',
                              [255*H/360, 255*L, 255*S, H, L, S]);

    RGBTripleToCMYK(RGBTriple, CC,MM,YY,KK);
    LabelCMYK.Caption := Format('[C, M, Y, K] = [%d, %d, %d, %d]',
                         [ CC,MM,YY,KK ]);

{// Windows API calls don't seem to work
    LabelCMYK.Caption := Format('[C, M, Y, K] = [%d, %d, %d, %d]',
                        [ GetCValue(ColorToRGB(RGB)),
                          GetMValue(RGB),
                          GetYValue(RGB),
                          GetKValue(RGB) ]);
}

    Intensity  := RGBTripleToIntensity(RGBTriple);
    LabelIntensity.Caption := Format('Intensity = %d = %.3f',
                                     [Intensity, Intensity/255]);

    Lightness := RGBTripleToLightness(RGBTriple);
    LabelLightness.Caption := Format('Lightness = %d = %.3f',
                                     [Lightness, Lightness/255]);

    // See [Foley96, pp. 589]
    LabelYIQ.Caption := Format('[Y, I, Q] = [%.3f,  %.3f,  %.3f]',
                               [(0.299*R + 0.587*G + 0.114*B) / 255,
                                (0.596*R - 0.275*G - 0.321*B) / 255,
                                (0.212*B - 0.523*G + 0.311*B) / 255 ])
  END
  ELSE BEGIN
    LabelLocation.Caption := '';
    LabelRGB.Caption := '';
    LabelHSV.Caption := '';
    LabelHLS.Caption := '';
    LabelCMYK.Caption := '';
    LabelIntensity.Caption := '';
    LabelLightness.Caption := '';
    LabelYIQ.Caption := '';
  END
end;



procedure TFormShow.FormCreate(Sender: TObject);
  VAR
    IniFile :  TIniFile;
{$IFDEF GIF}
    s       :  STRING;
{$ENDIF}
begin
{$IFDEF GIF}
  s := OpenPictureDialog.Filter + '|GIFs (*.gif)|*.gif';
  Insert('*.gif;',s, POS('(',s)+1);  // Put GIF in "All" selection
  Insert('*.gif;',s, POS('|',s)+1);
  OpenPictureDialog.Filter := s;

{$ENDIF}

  IniFile := TIniFile.Create(ChangeFileExt(ParamStr(0), '.INI'));
  TRY
    OpenPictureDialog.InitialDir := Inifile.ReadString(KeywordSetup,
                                                       KeywordDirectory,
                             ExtractFilePath(ParamStr(0)))
  FINALLY
    IniFile.Free
  END;

  ImageHistogram.Picture := NIL;


  // Allocate this bitmap so histograms still work without image present
  //{
   BitmapBig := TBitmap.Create;
  BitmapBig.Width  := ImageBig.Width;
  BitmapBig.Height := ImageBig.Height;
  BitmapBig.PixelFormat := pf24bit;
  ImageBig.Picture.Graphic := BitmapBig;
  //}
// {
 BitmapBig := TBitmap.Create;
  BitmapBig.Width  := ImageBig.Width;
  BitmapBig.Height := ImageBig.Height;
  BitmapBig.PixelFormat := pf24bit;
  ImageBig.Picture.Graphic := BitmapBig;
  // Image4.Picture.LoadFromFile(files);

  Bitmap1Big := TBitmap.Create;
  Bitmap1Big.Width  := Image4.Width;
  Bitmap1Big.Height := Image4.Height;
  Bitmap1Big.PixelFormat := pf24bit;
  //Image1.Picture.Graphic := BitmapBig;
   Image2.Picture.Graphic := BitmapBig;
    Image3.Picture.Graphic := BitmapBig;
    // Image4.Picture.Graphic := Bitmap1Big;
   {
    Bitmap3 := TBitmap.Create;
  Bitmap3.Width  := ImageBig.Width;
  Bitmap3.Height := ImageBig.Height;
  Bitmap3.PixelFormat := pf24bit;
  Image3.Picture.Graphic := BitmapBig;

      }
  BitmapPrint := TBitmap.Create;
  BitmapPrint.Width  := ImageBig.Width;
  BitmapPrint.Height := ImageBig.Height;
  BitmapPrint.PixelFormat := pf24bit;

   // }
          //xxxxx
end;

procedure TFormShow.RadioGroupColorPlaneClick(Sender: TObject);
begin
  ImageHistogram.Canvas.Brush.Color := clLtGray;
  // Clear old histogram
  ImageHistogram.Canvas.FillRect(ImageHistogram.Canvas.ClipRect);

  ColorPlane := cpIntensity;   // avoid compiler warning
  CASE RadioGroupColorSpace.ItemIndex OF
    0:  // csRGB
        BEGIN
          CASE RadioGroupColorPlane.ItemIndex OF
            0:  ColorPlane := cpRGB;
            1:  ColorPlane := cpRed;
            2:  ColorPlane := cpGreen;
            3:  ColorPlane := cpBlue
          END;
          CheckBoxMonochrome.Visible := TRUE
        END;

    1:  // csHSV
        BEGIN
          CASE RadioGroupColorPlane.ItemIndex OF
            0:  ColorPlane := cpHueHSV;
            1:  ColorPlane := cpSaturationHSV;
            2:  ColorPlane := cpValue
          END;
          CheckBoxMonochrome.Visible := (ColorPlane = cpHueHSV);
        END;

    2:  // csHLS
        BEGIN
          CASE RadioGroupColorPlane.ItemIndex OF
            0:  ColorPlane := cpHueHLS;
            1:  ColorPlane := cpLightness;
            2:  ColorPlane := cpSaturationHLS;
          END;
          CheckBoxMonochrome.Visible := (ColorPlane = cpHueHLS);
        END;

    3:  // csCMYK
        BEGIN
          CASE RadioGroupColorPlane.ItemIndex OF
            0:  ColorPlane := cpCyan;
            1:  ColorPlane := cpMagenta;
            2:  ColorPlane := cpYellow;
            3:  ColorPlane := cpBlack;
          END;
          CheckBoxMonochrome.Visible := FALSE;
        END;
  END;

  UpdateDisplay (ColorPlane);

  ButtonSave.Enabled := TRUE
end;


PROCEDURE TFormShow.UpdateDisplay (CONST ColorPlane:  TColorPlane);
  VAR
    BitmapProcessed,BitmapProcessed2:  TBitmap;
    Histogram      :  THistogram;
BEGIN
  Screen.Cursor := crHourGlass;

  TRY
    LabelColorPlane.Caption := GetColorPlaneString(ColorPlane);

    Histogram := THistogram.Create;
    TRY
      GetHistogram(BitmapBig, ColorPlane, Histogram);
      DrawHistogram(ColorPlane, Histogram, ImageHistogram.Canvas)
    FINALLY
      Histogram.Free
    END;

    BitmapProcessed := ExtractImagePlane(ColorPlane,
                                         CheckBoxMonochrome.Visible AND
                                         NOT CheckBoxMonochrome.Checked,
                                         CheckBoxInvert.Checked,   BitmapBig);
     BitmapProcessed2 := ExtractImagePlane(ColorPlane,
                                         CheckBoxMonochrome.Visible AND
                                         NOT CheckBoxMonochrome.Checked,
                                         CheckBoxInvert.Checked,
                                         Bitmap1Big);

    TRY
      //BitmapPrint.Assign(BitmapProcessed);
      ImageBig.Picture.Graphic := BitmapProcessed;
   Image4.Picture.Graphic := BitmapProcessed2;
   //       zzzz
    FINALLY
      BitmapProcessed.Free
    END
  FINALLY
    Screen.Cursor := crDefault
  END
END {UpdateDisplay};


procedure TFormShow.RadioGroupColorSpaceClick(Sender: TObject);
begin
  RadioGroupColorPlane.Items.Clear;

  CASE RadioGroupColorSpace.ItemIndex OF
    0:  // RGB
        BEGIN
          WITH RadioGroupColorPlane.Items DO
          BEGIN
            Add ('RGB Composite');
            Add ('Red');
            Add ('Green');
            Add ('Blue')
          END
        END;

    1:  // HSV
        BEGIN
          WITH RadioGroupColorPlane.Items DO
          BEGIN
            Add ('Hue');
            Add ('Saturation');
            Add ('Value')
          END
        END;

    2:  // HLS
        BEGIN
          WITH RadioGroupColorPlane.Items DO
          BEGIN
            Add ('Hue');
            Add ('Lightness');
            Add ('Saturation')
          END
        END;

    3:  // CMYK
        BEGIN
          WITH RadioGroupColorPlane.Items DO
          BEGIN
            Add ('Cyan');
            Add ('Magenta');
            Add ('Yellow');
            Add ('blacK')
          END
        END;
  END;

  RadioGroupColorPlane.ItemIndex := 0
end;


procedure TFormShow.FormDestroy(Sender: TObject);
begin
  BitmapPrint.Free;
  BitmapBig.Free;
   BitmapClear.Free;
  Bitmap1Big.Free;

end;



procedure TFormShow.ButtonPrintClick(Sender: TObject);
begin
  FormPrint.ShowModal
end;


procedure TFormShow.ButtonSaveClick(Sender: TObject);
begin
  IF   SavePictureDialog.Execute
  THEN Image1.Picture.Bitmap.SaveToFile(SavePictureDialog.Filename) ;
end;


procedure TFormShow.CheckBoxStretchClick(Sender: TObject);
begin
  ImageBig.Stretch := CheckBoxStretch.Checked;

  Image1.Stretch := CheckBoxStretch.Checked;
  Image3.Stretch := CheckBoxStretch.Checked;
  Image2.Stretch := CheckBoxStretch.Checked;
  Image4.Stretch := CheckBoxStretch.Checked;

end;


// A better way would be to trap CM_MouseLeave of the ImageBig TImage, but
// this requires a slightly modified version of a TImage.
procedure TFormShow.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  LabelLocation.Caption := '';
  LabelRGB.Caption := '';
  LabelHSV.Caption := '';
  LabelHLS.Caption := '';
  LabelCMYK.Caption := '';
  LabelIntensity.Caption := '';
  LabelLightness.Caption := '';
  LabelYIQ.Caption := ''
end;

procedure TFormShow.CheckBoxInvertClick(Sender: TObject);
begin
  UpdateDisplay (ColorPlane);
end;

procedure TFormShow.Button2Click(Sender: TObject);
var 
  x,y  : Integer;
  Line1,Line2 : PRGB32Array;
  YY : Byte;
begin

  with Image1.Picture.Bitmap do
  begin
    PixelFormat := pf24bit;//ImageBig.Picture.Bitmap.PixelFormat;
    Width := BitMapBig.Width;
    Height := BitMapBig.Height;
  end;
{
    for y := 0 to ImageBig.Height - 1 do
    begin
      Line1 := Image1.Picture.Bitmap.Scanline[y];
      Line2 := ImageBig.Picture.Bitmap.Scanline[y];
      for x := 0 to ImageBig.Width - 1 do
      begin
        Line1[x].B := Line2[x].B;
        Line1[x].G := Line2[x].G;
        Line1[x].R := Line2[x].R;
      end;
    end;
  Image1.Invalidate;


    Image1.Width := BitMapBig.Width;
    Image1.Height := BitMapBig.Height;
}
    for y := 0 to BitmapBig.Height - 1 do
    begin
      Line1 := Image1.Picture.Bitmap.Scanline[y];
//      Line2 := BitMapBig.Scanline[y];
      for x := 0 to BitMapBig.Width - 1 do
      begin
//         Image1.Canvas.Pixels[x,y] := BitMapBig.Canvas.Pixels[x,y];
        YY:=Round(0.299*Line1[x].R + 0.587*Line1[x].G + 0.114*Line1[x].B);

        If (YY/255) < 0.30 Then
        Begin
              Line1[x].B := 0;
              Line1[x].G := 0;
              Line1[x].R := 0;
        End
        Else
        Begin
              Line1[x].B := 255;
              Line1[x].G := 255;
              Line1[x].R := 255;

        End;
      end;
    end;

  Image1.Invalidate;
//    Image1.Refresh;
//    Image1.Stretch := CheckBoxStretch.Checked;
//    Image1.Width := ImageBig.Width;
//    Image1.Height := ImageBig.Height;

end;

procedure TFormShow.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
  VAR
    CC,MM,YY,KK:  INTEGER;      // CMYK variables
    Intensity  :  INTEGER;
    Lightness  :  INTEGER;
    RGB        :  TColor;
    RGBTriple  :  TRGBTriple;
    R,G,B      :  BYTE;
    H,S,V, L   :  TReal;
    xActual    :  INTEGER;
    yActual    :  INTEGER;
begin
(*  IF   CheckBoxStretch.Checked
  THEN BEGIN
    xActual := MulDiv(X, Bitmap1Big.Width,  Image1.Width);
    yActual := MulDiv(Y, Bitmap1Big.Height, Image1.Height)
  END
  ELSE BEGIN            
    xActual := X;
    yActual := Y
  END;

  // If bitmap is smaller than image area, only show values if inside bitmap.
  IF   (xActual < Image1.Picture.Bitmap.Width) AND
       (yActual < Image1.Picture.Bitmap.Height)
  THEN BEGIN

    LabelLocation.Caption :=  '[I, J] = [' + IntToStr(xActual) + ', ' +
                                             IntToStr(yActual) + ']';

    // Get values from original bitmap, which is not necessarily being displayed
    // in RGB format.  Use Pixels so this will work with pf8bit or pf24bit BMPs.
    RGB :=  Bitmap1Big.Canvas.Pixels[xActual, yActual];
    R := GetRValue(RGB);
    G := GetGValue(RGB);
    B := GetBValue(RGB);
    RGBTriple := ColorToRGBTriple(RGB);

    LabelRGB.Caption := Format('[R, G, B] = [%d, %d, %d] = [%.3f,  %.3f,  %.3f]',
                        [ R, G, B, R/255, G/255, B/255]);

    RGBToHSV(GetRValue(RGB) / 255,
             GetGValue(RGB) / 255,
             GetBValue(RGB) / 255, H, S, V);
    LabelHSV.Caption := Format('[H, S, V] = [%.0f, %.0f, %.0f] = [ %.1f,  %.3f,  %.3f]',
                              [255*H/360, 255*S, 255*V, H, S, V]);

    RGBToHLS(GetRValue(RGB) / 255,
             GetGValue(RGB) / 255,
             GetBValue(RGB) / 255, H, L, S);
    LabelHLS.Caption := Format('[H, L, S] = [%.0f, %.0f, %.0f] = [ %.1f,  %.3f,  %.3f]',
                              [255*H/360, 255*L, 255*S, H, L, S]);

    RGBTripleToCMYK(RGBTriple, CC,MM,YY,KK);
    LabelCMYK.Caption := Format('[C, M, Y, K] = [%d, %d, %d, %d]',
                         [ CC,MM,YY,KK ]);

{// Windows API calls don't seem to work
    LabelCMYK.Caption := Format('[C, M, Y, K] = [%d, %d, %d, %d]',
                        [ GetCValue(ColorToRGB(RGB)),
                          GetMValue(RGB),
                          GetYValue(RGB),
                          GetKValue(RGB) ]);
}

    Intensity  := RGBTripleToIntensity(RGBTriple);
    LabelIntensity.Caption := Format('Intensity = %d = %.3f',
                                     [Intensity, Intensity/255]);

    Lightness := RGBTripleToLightness(RGBTriple);
    LabelLightness.Caption := Format('Lightness = %d = %.3f',
                                     [Lightness, Lightness/255]);

    // See [Foley96, pp. 589]
    LabelYIQ.Caption := Format('[Y, I, Q] = [%.3f,  %.3f,  %.3f]',
                               [(0.299*R + 0.587*G + 0.114*B) / 255,
                                (0.596*R - 0.275*G - 0.321*B) / 255,
                                (0.212*B - 0.523*G + 0.311*B) / 255 ])
  END
  ELSE BEGIN
    LabelLocation.Caption := '';
    LabelRGB.Caption := '';
    LabelHSV.Caption := '';
    LabelHLS.Caption := '';
    LabelCMYK.Caption := '';
    LabelIntensity.Caption := '';
    LabelLightness.Caption := '';
    LabelYIQ.Caption := '';
  END
*)

end;

procedure TFormShow.Button3Click(Sender: TObject);
var
  Width,Height,x,y :Integer;
  Line1,Line2,Line3,Line4 :PRGB32Array;
begin
  with  Image1.Picture.Bitmap do
  begin
    PixelFormat := pf32bit;

    Width := 510;
    Height := 510;
    for y:=1 to Height-1 do
    begin
      Line1 :=ScanLine[0];
      Line2 :=ImageBig.picture.Bitmap.scanline[y];
      for x:=1 to width-1 do
        begin
              Line1[x].R:=Line2[x].R;
              Line1[x].G:=Line2[x].G;
              Line1[x].B:=Line2[x].B;
        end;
end;
  end;
  //{
   Image1.Invalidate;

with  Image4.Picture.Bitmap do
  begin
    PixelFormat := pf32bit;

    Width := 512;
    Height := 512;
    for y:=1 to Height-1 do
    begin
      Line3 :=ScanLine[0];
      Line4 :=Image4.picture.Bitmap.scanline[y];
      for x:=1 to width-1 do
        begin
              Line3[x].R:=Line4[x].R;
              Line3[x].G:=Line4[x].G;
              Line3[x].B:=Line4[x].B;
        end;
end;
  end;
   Image2.Invalidate;
  

end;



procedure TFormShow.ScrollBar1Change(Sender: TObject);
VAR

    Filename,Filename2,str,str2:  STRING;
    num,yy:Integer;
  {   IniFile :  TIniFile;
    NewPath :  STRING; }
begin
      num:=ScrollBar1.Position;
      str:= IntToStr(num);
      yy:=num+1;
      str2:= IntToStr(yy);
    Filename:='C:\Users\gb\Desktop\radarProcess\imageClear\a ('+str+').bmp';
    Filename2:='C:\Users\gb\Desktop\radarProcess\imageClear\a (1).bmp';
   // Filename2:='C:\Users\gb\Desktop\radarProcess\imageClear\a ('+str2+').bmp';
        //Filename:='C:\Users\User\Desktop\radarProcess\imageClear\a ('+str+').bmp';
    BitmapBig.Free;
    BitmapBig := TBitmap.Create;
    BitmapBig := LoadGraphicsFile(Filename);


  // for the computation of Image1
    Bitmap1Big.Free;
    Bitmap1Big := TBitmap.Create;
    Bitmap1Big := LoadGraphicsFile(Filename2);
      Image1.Picture.LoadFromFile(Filename);
      Image4.Picture.LoadFromFile(Filename2);
    Image2.Picture.LoadFromFile(Filename2);
     // Image3.Picture.LoadFromFile(Filename);

    //Image1.Picture.LoadFromFile(OpenPictureDialog.Filename);

    // Flush INI cache
    WritePrivateProfileString(NIL, NIL, NIL, pChar(Filename));

    LabelFilename.Caption := Filename;

    // Update this label before forcing PixelFormat
    LabelAttributes.Caption := GetBitmapDimensionsString(BitmapBig);

    // Force to pf24bit if necessary.  Make other logic much more simple by
    // not having to worry aboug various PixelFormats.
    IF   BitmapBig.PixelFormat <> pf24bit
    THEN BitmapBig.PixelFormat := pf24bit;
    IF   Bitmap1Big.PixelFormat <> pf24bit
    THEN Bitmap1Big.PixelFormat := pf24bit;

    // Clear old histogram (some of the following may be unnecessary now that
    // pf24bit is being forced and it's no longer necessary to hide certain
    // features.
   // ImageHistogram.Visible := TRUE;
   { ImageHistogram.Picture := NIL;
    RadioGroupColorSpace.ItemIndex := 0;  {Select RGB color space}
  {  RadioGroupColorSpace.Enabled := TRUE;
    RadioGroupColorPlane.Enabled := TRUE;   }
    ButtonPrint.Enabled := TRUE;
    FormPrint.ButtonHistograms.Enabled := TRUE;
    FormPrint.ButtonColorSpaceArray.Enabled := TRUE;
    RadioGroupColorSpace.Visible := TRUE;
    RadioGroupColorPlane.Visible := TRUE;
    UpdateDisplay(cpRGB);
    Button3Click(Sender);
     Button6Click(Sender);

    LabelUniqueColors.Caption := 'Unique Colors = ' +
                                 IntToStr( CountColors(BitmapBig) );

end;




procedure TFormShow.Button6Click(Sender: TObject);
var
  Width,Height,x,y ,z:Integer;
  Line1,Line2,Line3,Line4 :PRGB32Array;
  Filename,str:  STRING;
  myarr: Array [1..5] of Integer;
begin

  with  Image3.Picture.Bitmap do
  begin
    PixelFormat := pf32bit;

    Width := 510;
    Height := 510;
    for y:=1 to Height-1 do
    begin
      Line1 :=ScanLine[y];
      Line2 :=Image1.picture.Bitmap.scanline[y];
	  Line3	:=Image4.picture.Bitmap.scanline[y];
       for x:=1 to width-1 do
       //  11ggggggggggggggggggggggi
    begin
        //  z:=abs(x-y);

	if(	abs(Line2[x].R-Line3[x].R)<55)and(abs(Line2[x].G-Line3[x].G)<55)and(abs(Line2[x].B-Line3[x].B)<55) then
  begin
        Line1[x].R:=255;
        Line1[x].G:=255;
        Line1[x].B:=255;
  end
  else
  begin
         Line1[x].R:=Line2[x].R ;
         Line1[x].G:=Line2[x].G ;
         Line1[x].B:=Line2[x].B ;

  end
  
        end;

    end;
  end;
   Image3.Invalidate;
   //                    }
end;
//        }
procedure TFormShow.Button5Click(Sender: TObject);
var
  Width,Height,x,y :Integer;
  Line1,Line2,Line3,Line4 :PRGB32Array;
  xx:Byte;
  Filename,str:  STRING;
begin
 with  Image3.Picture.Bitmap do
  begin
    PixelFormat := pf32bit;

    Width := 510;
    Height := 510;
    for y:=1 to Height-1 do
    begin
      Line1 :=ScanLine[y];
      Line2 :=Image1.Picture.Bitmap.ScanLine[y];
      Line3 :=Image2.Picture.Bitmap.ScanLine[y];

       for x:=1 to width-1 do
    begin
    //if	(Line2[x].R	=	0	)and	(Line2[x].G	=		248	)and	(Line2[x].B	=			0	) then
        // xx:=Line3[x].R;
  // if	xx	=	0	then
  if	(Line2[x].R	<>Line3[x].R)and	(Line2[x].G	<>		Line3[x].G	)and	(Line2[x].B	<>		Line3[x].B		) then
    //if	(2=2) then
					begin

					    Line1[x].R:= Line2[x].R;
						  Line1[x].G:= Line2[x].G;
					  	Line1[x].B:= Line2[x].B;
					end
    else
					begin
						Line1[x].R:=0;
						Line1[x].G:=0;
						Line1[x].B:=Line2[x].B;//999
					end
    end;

    end;
    end;
   Image2.Invalidate;

end;
procedure TFormShow.Button7Click(Sender: TObject);
var
  Width,Height,x,y,z,rrr,ggg,bbb,count,num ,countsm:Integer;
  Line1,Line2,Line3,Line4 :PRGB32Array;
  rr,gg,bb:Byte;
  Filename,str:  STRING;
  myarr: Array [0..50000] of Byte;
  myarg: Array [0..50000] of Byte;
  myarb: Array [0..50000] of Byte;
begin

  Memo1.Clear;

 {
for x:=1 to 510 do
  begin
  Line1:= ImageBig.Picture.Bitmap.ScanLine[x];
    rr:=Line1[50].R;
    gg:=Line1[50].G;
    bb:=Line1[50].B;
   // rr:=5;
    rrr:=rr;
    ggg:=gg;
    bbb:=bb;
   // str:=  IntToStr(rr)+' '+IntToStr(gg)+' '+IntToStr(bb)+sLineBreak;
    str:=  IntToStr(Line1[50].R)+' '+IntToStr(Line1[50].G)+' '+IntToStr(Line1[50].B)+sLineBreak;
     Memo1.Text:=Memo1.Text+str;
  end;
      }
      count:=0;
      countsm:=0;
      myarr[count]:=0;
      myarg[count]:=0;
      myarb[count]:=0;
      rr:=0;
      gg:=0;
      bb:=0;
 with  Image3.Picture.Bitmap do
  begin
    PixelFormat := pf32bit;

    Width := 510;
    Height := 510;
    for y:=1 to Height-1 do
    begin
      Line1 :=ScanLine[y];
      Line2 :=Image1.Picture.Bitmap.ScanLine[y];
     // Line3 :=Image2.Picture.Bitmap.ScanLine[y];
    for x:=1 to width-1 do
    begin


// if	(x=20 )	or (x=38 )	or (x= 270)	or (x=380 )	or (x=457 )	or (x=70 )or (x=80 )or (x=150	) then
 //begin
 countsm:=0;
       num:=count;
 for  z:=0 to num do
 begin
                if( Line2[x].R= myarr[z]) and (Line2[x].G=myarg[z] ) and(Line2[x].B =myarb[z])      then
              begin
                  countsm:=countsm+1;
              end


end;
                   if(countsm=0)then
                	begin
                {
                  str:=  IntToStr(Line2[x].R)+' '+IntToStr(Line2[x].G)+' '+IntToStr(Line2[x].B)+sLineBreak;
     Memo1.Text:=Memo1.Text+str;
     }
                  // rr :=    Line2[x].R;
                  // gg :=    Line2[x].G;
                  // bb :=    Line2[x].B;
                  //  str:=  IntToStr(myarr[z])+' '+IntToStr(myarg[z])+' '+IntToStr(myarb[z])+' '+IntToStr(z)+sLineBreak
                   // +IntToStr(Line2[x].R)+' '+IntToStr(Line2[x].G)+' '+IntToStr(Line2[x].B)+sLineBreak;
                     // Memo1.Text:=Memo1.Text+str;
                                 count:=count+1;

                      myarr[count]:=Line2[x].R;
                      myarg[count]:=   Line2[x].G;
                      myarb[count]:=   Line2[x].B;
                      str:=  IntToStr(myarr[count])+' '+IntToStr(myarg[count])+' '+IntToStr(myarb[count])+' '+IntToStr(count)+sLineBreak;
     Memo1.Text:=Memo1.Text+str;
                  //   Break;
                  end


                  else
                  begin
                       Line1[x].R:= 255;
                  Line1[x].G:= 255;
                  Line1[x].B:= 255;




              end

				 //	end
   // else
			 //		begin
					//	Line1[x].R:= Line2[x].R;
						//Line1[x].G:= Line2[x].G;
						//Line1[x].B:= Line2[x].B;
				 //	end
        // end
       // end;
    end;
    end;
    end;
   Image3.Invalidate;
   //Memo1.Text:=   IntToStr(count);
end;


procedure TFormShow.Button4Click(Sender: TObject);
begin
       IF   SavePictureDialog.Execute
  THEN Image1.Picture.Bitmap.SaveToFile(SavePictureDialog.Filename) ;
end;

procedure TFormShow.Button8Click(Sender: TObject);
begin
        IF   SavePictureDialog.Execute
  THEN Image3.Picture.Bitmap.SaveToFile(SavePictureDialog.Filename) ;
end;





































end.
